﻿using System.Drawing.Printing;
using UnityEngine;

namespace ZyGame.Game
{
    public sealed class EntityContext : MonoBehaviour
    {
        public Entity entity;
    }

    public class TransformComponent : EntityComponent
    {
        public GameObject gameObject { get; private set; }

        public Transform transform
        {
            get { return gameObject == null ? null : gameObject.transform; }
        }

        public Vector3 position
        {
            get => transform.position;
            set => transform.position = value;
        }

        public Vector3 rotation
        {
            get => transform.rotation.eulerAngles;
            set => transform.rotation = Quaternion.Euler(value);
        }

        public Vector3 scale
        {
            get => transform.localScale;
            set => transform.localScale = value;
        }

        public override void Awake(params object[] dataList)
        {
            string assetPath = dataList.GetAndCast<string>(0);
            if (assetPath.IsNullOrEmpty() is false)
                gameObject = Client.Resource.LoadAsset<GameObject>(assetPath);
            else
                gameObject = dataList.GetAndCast<GameObject>(0);
            EntityContext context = gameObject.GetOrAddComponent<EntityContext>();
            context.entity = entity;
            position = dataList.CastOrDefault<Vector3>(1, Vector3.zero);
            rotation = dataList.CastOrDefault<Vector3>(2, Vector3.zero);
            scale = dataList.CastOrDefault<Vector3>(3, Vector3.one);
        }

        public override void Dispose()
        {
            if (gameObject is null)
            {
                return;
            }

            GameObject.DestroyImmediate(gameObject);
        }
    }
}